﻿using System;
using System.Security.Cryptography;
using System.Text;

using Moe.ECamel.Common.Entities;

namespace Moe.ECamel.Common.Security
{
	/// <summary>
	/// ECamel Password hasher.
	/// Passwords are hashed for enhanced security, so cleartext password is not stored in DB or passed from the client to the server.
	/// </summary>
	public static class ECamelPasswordHasher
	{
		private static readonly HashAlgorithm _hashAlgorithm;

		static ECamelPasswordHasher()
		{
			_hashAlgorithm = HashAlgorithm.Create("SHA1");
		}

		public static byte[] ComputeHash(String password)
		{
			if (password == null)
				password = String.Empty;
			byte[] passwordBytes = Encoding.Unicode.GetBytes(password);
			byte[] hash = _hashAlgorithm.ComputeHash(passwordBytes);
			return hash;
		}

		public static void GenerateDefaultPassword(User user)
		{
			user.HashedPassword = ComputeHash(user.Username);
		}
	}
}
